package com.jcf.springboot.security;

/**
 * @author test
 * @since 2022/7/29
 */

import com.jcf.springboot.security.filter.JwtAuthenticationEntryPoint;
import com.jcf.springboot.security.filter.JwtAuthenticationFilter;
import com.jcf.springboot.security.service.MyUserdetailService;
import com.jcf.springboot.security.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private MyUserdetailService userDetailsService;
  @Autowired
  private JwtUtil jwtUtil;
  @Autowired
  private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;

  public static final String[] WHITE_LIST = {
   "/**/upload",
   "/**/login",
   "/image/**",
   "/**/*swagger*/**",
   "/**/*api*","/doc.html","/webjars/**","/ws/**","/**/refreshToken"
  };



  public AuthenticationManager authenticationManager() throws Exception {
    return super.authenticationManager();
  }


  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
/*    auth.inMemoryAuthentication()
     .withUser("admin").password(passwordEncoder().encode("123")).roles("admin");*/
  }

  //密码编码器
  @Bean
  public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
  }

  @Bean
  JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
    return new JwtAuthenticationFilter(authenticationManager());
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    //todo:1.退出登录后，token问题
    // 2.token失效后的重新登录
    http
     .cors()
     .and()
     .csrf()
     .disable()
     // session禁用配置
     .sessionManagement()
     .sessionCreationPolicy(SessionCreationPolicy.STATELESS)  // 无状态

     // 拦截规则配置
     .and()
     .authorizeRequests()
     .antMatchers(WHITE_LIST
      ).permitAll()  // 白名单 放行
     .anyRequest().authenticated()

     .and()
     .exceptionHandling()
     .authenticationEntryPoint(jwtAuthenticationEntryPoint)
      .and()
      .addFilter(jwtAuthenticationFilter());



  }

}
